﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.ComponentModel;
using System.Diagnostics;

namespace CPAScoringPerformanceTester.Models
{
    class Options : INotifyPropertyChanged
    {
        #region Fields
        private const string FILE_OPTIONS_CONFIG = "plan-options.xml";
        private const string OPTION_SERVICE_URL = "service-url";
        private const string OPTION_THREAD_FREQUENCY = "thread-frequency";
        public event PropertyChangedEventHandler PropertyChanged;
        public static Options Current = new Options();
        #endregion

        #region Properties
        private string serviceUrl;
        public string ServiceUrl
        {
            get { return serviceUrl; }
            set
            {
                if (value == serviceUrl) return;
                serviceUrl = value;
                OnPropertyChanged("ServiceUrl");
            }
        }

        private int threadFrequency;
        public int ThreadFrequency
        {
            get { return threadFrequency; }
            set
            {
                if (value == threadFrequency || value < 0) return;
                threadFrequency = value;
                OnPropertyChanged("ThreadFrequency");
            }
        }
        #endregion

        #region Constructor
        private Options()
        { 
        
        }
        #endregion

        #region Public Methods
        public bool Load()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FILE_OPTIONS_CONFIG);

                ServiceUrl = GetOptionValue(doc, OPTION_SERVICE_URL);
                ThreadFrequency = Convert.ToInt32(GetOptionValue(doc, OPTION_THREAD_FREQUENCY));

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        public bool SaveDefault()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FILE_OPTIONS_CONFIG);

                SetOptionValue(doc, OPTION_SERVICE_URL, ServiceUrl);
                SetOptionValue(doc, OPTION_THREAD_FREQUENCY, ThreadFrequency);

                doc.Save(FILE_OPTIONS_CONFIG);

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }
        #endregion

        #region Private Methods
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string GetOptionValue(XmlDocument doc, string name)
        {
            XmlNode node = doc.SelectSingleNode(string.Format("/options-config/{0}", name));
            return node.InnerText;
        }

        private void SetOptionValue(XmlDocument doc, string name, object value)
        {
            XmlNode node = doc.SelectSingleNode(string.Format("/options-config/{0}", name));
            node.InnerText = value.ToString();
        }
        #endregion
    }
}
